<?php

namespace App\Http\Controllers\V1\API;

use App\Http\Controllers\V1\API\BaseController;
use App\Models\V1\NewArrival;
use App\Models\V1\NewStudent;
use App\Models\V1\Timetable;
use App\Models\V1\ReceiveStatus;
use App\Models\V1\Clothes;
use App\Models\V1\Pays;
use App\Models\V1\DailyUse;
use App\Models\V1\Textbook;
use http\Client\Curl\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Auth;
use Log;

class TimetableController extends BaseController
{
    /***
     * 获取日程安排数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getTimetables(Request $request) {
        // 自定义返回格式
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
            'data' => array()
        );
        $timetables = $this->getTimetableList();
        if ($timetables->isEmpty()) {
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_NO_CONTENT];
        } else {
            // 格式化数据
            $resultClothes = array();
            foreach ($timetables as $item) {
                $formatTime = array();
                $formatTime['timetableCode'] = $item['code'];
                $formatTime['timetableName'] = $item['name'];
                $formatTime['timetableCategory'] = $item['category'];
                $formatTime['time'] = isset($item['time']) ? $item['time'] : '';
                $formatTime['detail'] = isset($item['detail']) ? $item['detail'] : '';
                $formatTime['address'] = isset($item['address']) ? $item['address'] : '';
                $formatTime['reception'] = isset($item['reception_person']) ? $item['reception_person'] : '';
                $formatTime['attention'] = isset($item['attention']) ? $item['attention'] : '';
                $relation = $item['relate_information'];
                if (!empty($relation) || isset($relation)) {
                    $relation = json_decode($relation, true);
                }
                $formatTime['relation'] = $relation;
                array_push($resultClothes, $formatTime);
            }
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_OK];
            $result['data'] = $resultClothes;
        }
        return $this->sendResponse($result, $result['errorCode']);
    }

    /***
     * 获取校服列表
     * @param $code
     * @return mixed
     */
    private function getTimetableList(){
        $arrival_code = $this->getArrivalCode();
        if (strlen($arrival_code)>0){
            $data = Timetable::where('arrival_code',$arrival_code)->orderBy('show_order','asc')->get();
        }else {
            $data = Timetable::orderBy('show_order','asc')->all();
        }
        return $data;
    }

    /***
     * 获取当前的迎新代码
     * @return array
     */
    private function getArrivalCode()
    {
        $code = '';
        $codes = NewArrival::where('isOn',true)->first();
        if (!empty($codes) || isset($codes)) {
            $code = $codes->code;
        }
        return $code;
    }
    
    //获取日程所属相关类别
    public function getTimetableCategory(Request $request){
        // 自定义返回格式
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
            'data' => array()
        );
        $reqParams = $request -> all();
        //获取code
        $code = isset($reqParams['code']) ? $reqParams['code'] : '';
        $timeTableCategory = $this->getTimetableCategoryList($reqParams);
        if ($timeTableCategory->isEmpty()) {
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_NO_CONTENT];
        } else {
            // 定义返回数组
            $resultData = array();
            if($code == '生活用品'){
                // 格式化请求数据
                foreach ($timeTableCategory as $item) {
                    $formatTime = array();
                    $formatTime['code'] = $item['code'];
                    $formatTime['name'] = $item['name'];
                    $formatTime['remark'] = $item['remark'];
                    $formatTime['count'] = $item['count'];
                    $formatTime['unit'] = $item['unit'];
                    $formatTime['sex_limit'] = $item['sex_limit'];
                    $formatTime['arrival_code'] = $item['arrival_code'];
                    array_push($resultData, $formatTime);
                }
                
            }elseif($code == '新生教材'){
                // 格式化请求数据
                foreach ($timeTableCategory as $item) {
                    $formatTime = array();
                    $formatTime['code'] = $item['code'];
                    $formatTime['name'] = $item['name'];
                    $formatTime['remark'] = $item['remark'];
                    $formatTime['count'] = $item['count'];
                    $formatTime['unit'] = $item['unit'];
                    $formatTime['majors'] = $item['majors'];
                    $formatTime['arrival_code'] = $item['arrival_code'];
                    array_push($resultData, $formatTime);
                }
                
            }elseif($code == '校服清单'){
                // 格式化请求数据
                foreach ($timeTableCategory as $item) {
                    $formatTime = array();
                    $formatTime['cloth_code'] = $item['code'];
                    $formatTime['name'] = $item['name'];
                    $formatTime['picture'] = $item['picture'];
                    $formatTime['remark'] = $item['remark'];
                    $formatTime['count'] = $item['count'];
                    $formatTime['unit'] = $item['unit'];
                    $formatTime['sex_limit'] = $item['sex_limit'];
                    $formatTime['arrival_code'] = $item['arrival_code'];
                    array_push($resultData, $formatTime);
                }
                
            }elseif($code == '费用类别'){
                // 格式化请求数据
                foreach ($timeTableCategory as $item) {
                    $formatTime = array();
                    $formatTime['pay_code'] = $item['code'];
                    $formatTime['name'] = $item['name'];
                    $formatTime['remark'] = $item['remark'];
                    $formatTime['amount'] = $item['amount'];
                    $formatTime['unit'] = $item['unit'];
                    $formatTime['type'] = $item['type'];
                    $formatTime['sub_amount'] = $item['sub_amount'];
                    $formatTime['arrival_code'] = $item['arrival_code'];
                    array_push($resultData, $formatTime);
                }
                
            }
            
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_OK];
            $result['data'] = $resultData;
        }
        return $this->sendResponse($result, $result['errorCode']);
    }
    
    private function getTimetableCategoryList(array $params){
        //获取code
        $code = isset($params['code']) ? $params['code'] : '';

        //获取登陆人的批次代码
        $user = Auth::guard('api')->user();
        $user_code = $user['code'];
        $arrival_code = $this->getUserArrivalCode($user_code);
        if($code == '生活用品'){
            $data = DailyUse::where('arrival_code',$arrival_code)->get();
        }elseif($code == '新生教材'){
            $data = Textbook::where('arrival_code',$arrival_code)->get();
        }elseif($code == '校服清单'){
            $data = Clothes::where('arrival_code',$arrival_code)->get();
        }elseif($code == '费用类别'){
            $data = Pays::where('arrival_code',$arrival_code)->get();
        }
        return $data;
    }


    /***
     * 获取当前的迎新代码
     * @return array
     */
    private function getUserArrivalCode($code)
    {
        $student = $this->getStudentInfo($code);
        return $student['arrival_code'];
    }

    /***
     * 根据考试号获取学生基础信息
     * @param $code
     */
    private function getStudentInfo($code)
    {
        $student = NewStudent::where('exam_code', $code)->first();
        return $student;
    }
}
